<template>
  <div class="animated fadeIn">
    <div class="card card-accent-primary">
      <div class="card-header">
        <i class="fa fa-align-justify"></i> 分组列表
      </div>
      <div class="card-body">
        <div style="margin-bottom: 10px;">
          <Button type="warning" @click="add">添加分组</Button>
        </div>
        <i-table :loading="loading" border :columns="columns" :data="items"></i-table>
        <div style="margin: 10px;overflow: hidden">
          <Page :total="total" :page-size="10" @on-change="onChange" show-total></Page>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'dashboard',
  data () {
    return {
      key: '',
      columns: [
        {
          title: 'ID',
          key: 'ag_id',
          width: 50
        },
        {
          title: '用户组名称',
          key: 'ag_title',
          width: 200
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          className: 'table-min-width',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.$router.push({name: 'authGroupEdit', params: {id: params.row.ag_id}})
                  }
                }
              }, '修改'),
              h('Button', {
                props: {
                  type: 'error',
                  size: 'small'
                },
                on: {
                  click: () => {
                    this.deleteItem(params.row.ag_id)
                  }
                }
              }, '删除')
            ])
          }
        }
      ],
      items: [],
      total: 0,
      page: 1,
      loading: true
    }
  },
  methods: {
    onChange (page) {
      this.page = page
      this.getList()
    },
    // 获取列表
    getList (key) {
      this.$get('/api/admin/authGroup', {
        params: {
          page: this.page,
          key: key
        }
      }).then((res) => {
        if (res) {
          this.total = res.data.total
          this.items = res.data.data
          this.loading = false
        }
      })
    },
    // 删除
    deleteItem (id) {
      this.$Modal.confirm({
        title: '提醒',
        content: '确认要删除吗?',
        onOk: () => {
          this.$_delete('/api/admin/authGroup/', id).then((res) => {
            if (res) {
              this.$Message.success({
                content: res.mess,
                onClose: () => {
                  this.getList()
                }
              })
            }
          })
        }
      })
    },
    // 添加跳转
    add () {
      this.$router.push({name: 'authGroupAdd'})
    }
  },
  created () {
    this.getList()
  }
}
</script>
<style>
  /*解决table表格适应问题*/
  .table-min-width {
    min-width: 200px;
    width: 200px;
  }
</style>
